#Boa:Dialog:CodSussan

import wx
import wx.grid
import consultasBD
import utiles
from decimal import Decimal

def create(parent):
    return CodSussan(parent)

[wxID_CODSUSSAN, wxID_CODSUSSANGRID1, wxID_CODSUSSANSTATICTEXT1, 
 wxID_CODSUSSANSTATICTEXT2, wxID_CODSUSSANSTATICTEXT3, 
 wxID_CODSUSSANSTATICTEXT4, wxID_CODSUSSANSTATICTEXT5, 
 wxID_CODSUSSANTEXTCTRL1, wxID_CODSUSSANTEXTCTRL2, wxID_CODSUSSANTEXTCTRL3, 
 wxID_CODSUSSANTEXTCTRL4, wxID_CODSUSSANTEXTCTRL5, 
] = [wx.NewId() for _init_ctrls in range(12)]

class CodSussan(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_CODSUSSAN, name='CodSussan',
              parent=prnt, pos=wx.Point(211, 146), size=wx.Size(400, 324),
              style=wx.DEFAULT_DIALOG_STYLE, title='Seleccione la factura')
        self.SetClientSize(wx.Size(392, 297))

        self.staticText1 = wx.StaticText(id=wxID_CODSUSSANSTATICTEXT1,
              label='ID', name='staticText1', parent=self, pos=wx.Point(16, 8),
              size=wx.Size(11, 13), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_CODSUSSANTEXTCTRL1,
              name='textCtrl1', parent=self, pos=wx.Point(16, 24),
              size=wx.Size(72, 21), style=0, value='')
        self.textCtrl1.SetEditable(False)

        self.staticText2 = wx.StaticText(id=wxID_CODSUSSANSTATICTEXT2,
              label='Nombre', name='staticText2', parent=self, pos=wx.Point(88,
              8), size=wx.Size(37, 13), style=0)

        self.textCtrl2 = wx.TextCtrl(id=wxID_CODSUSSANTEXTCTRL2,
              name='textCtrl2', parent=self, pos=wx.Point(88, 24),
              size=wx.Size(208, 21), style=0, value='')
        self.textCtrl2.SetEditable(False)

        self.staticText3 = wx.StaticText(id=wxID_CODSUSSANSTATICTEXT3,
              label='Cod.Sussan', name='staticText3', parent=self,
              pos=wx.Point(16, 48), size=wx.Size(57, 13), style=0)

        self.textCtrl3 = wx.TextCtrl(id=wxID_CODSUSSANTEXTCTRL3,
              name='textCtrl3', parent=self, pos=wx.Point(16, 64),
              size=wx.Size(72, 21), style=0, value='')
        self.textCtrl3.SetEditable(False)

        self.grid1 = wx.grid.Grid(id=wxID_CODSUSSANGRID1, name='grid1',
              parent=self, pos=wx.Point(16, 88), size=wx.Size(360, 200),
              style=0)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnDobleClick)

        self.staticText4 = wx.StaticText(id=wxID_CODSUSSANSTATICTEXT4,
              label='Cod.Art.Prov.', name='staticText4', parent=self,
              pos=wx.Point(304, 8), size=wx.Size(68, 13), style=0)

        self.textCtrl4 = wx.TextCtrl(id=wxID_CODSUSSANTEXTCTRL4,
              name='textCtrl4', parent=self, pos=wx.Point(304, 24),
              size=wx.Size(72, 21), style=0, value='')
        self.textCtrl4.SetEditable(False)

        self.staticText5 = wx.StaticText(id=wxID_CODSUSSANSTATICTEXT5,
              label='Descripcion', name='staticText5', parent=self,
              pos=wx.Point(88, 48), size=wx.Size(54, 13), style=0)

        self.textCtrl5 = wx.TextCtrl(id=wxID_CODSUSSANTEXTCTRL5,
              name='textCtrl5', parent=self, pos=wx.Point(88, 64),
              size=wx.Size(288, 21), style=0, value='')
        self.textCtrl5.SetEditable(False)

    def __init__(self, parent, datos):
        self._init_ctrls(parent)
        self.textCtrl1.SetLabel(datos["idproveedor"])
        self.textCtrl2.SetLabel(datos["nombre"])
        self.textCtrl3.SetLabel(datos["codsussan"])
        self.textCtrl4.SetLabel(datos["codartprov"])
        self.textCtrl5.SetLabel(datos["descripcion"])
        self.datosRetorno = {"idfactura":"","costo":"","fechafactura":"","nrofactura":"","en1oen2":""}
        self.crearGrilla()
        self.llenarGrilla()

    def ajustarAnchoColumnas(self):
        for y in xrange(self.grid1.GetNumberCols()):
            self.grid1.AutoSizeColumn(y,True)

    def crearGrilla(self):
        #Fecha,NroDocumento,id,Tipo,CostoUnidad
        #0     1            2  3    4
        columnas = ['Fecha','Nro\nDoc','id','tipo','Costo']
        self.grid1.CreateGrid(1,len(columnas))
        for i, col in enumerate(columnas):
            self.grid1.SetColLabelValue(i, col)
        self.grid1.EnableEditing(False)
        self.ajustarAnchoColumnas()
        self.grid1.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
        self.grid1.Refresh()

    def buscarDatos(self,codsussan):
        r1 = "SELECT p1.nrofactura,p1.fechafactura, p1.idfactura,"
        r2 = "       p1.en1oen2,p2.costounidad "
        r3 = "FROM compras p1, compras_items p2 "
        r4 = "WHERE p2.codsussan = '"+codsussan+"' "
        r5 = "      AND p2.idfactura = p1.idfactura "
        r6 = "GROUP BY p1.idfactura "
        r7 = "ORDER BY p2.costounidad DESC LIMIT 1 "
        sql = r1+r2+r3+r4+r5+r6#+r7
        resultados = consultasBD.ordenCrudaDict(sql)
        return resultados
    
    def llenarGrilla(self):
        datos = self.buscarDatos(self.textCtrl3.GetLabel())
        #Fecha,NroDocumento,id,Tipo,CostoUnidad
        #0     1            2  3    4
        for fil,item in enumerate(datos):
            fecha = str(item["fechafactura"])[:10]
            fecha = utiles.invertirFecha2(fecha)
            nrodoc = str(item["nrofactura"])
            idfactura = str(item["idfactura"])
            tipo = str(item["en1oen2"])
            costo = str(item["costounidad"])
            self.grid1.SetCellValue(fil,0,fecha)
            self.grid1.SetCellAlignment(fil, 0, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(fil,1,nrodoc)
            self.grid1.SetCellAlignment(fil, 1, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(fil,2,idfactura)
            self.grid1.SetCellAlignment(fil, 2, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(fil,3,tipo)
            self.grid1.SetCellAlignment(fil, 3, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(fil,4,costo)
            self.grid1.SetCellAlignment(fil, 4, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.AppendRows()
        self.ajustarAnchoColumnas()
            
    def getDocumento(self):
        return self.datosRetorno

    def OnDobleClick(self, event):
        fila = self.grid1.GetGridCursorRow()
        #Fecha,NroDocumento,id,Tipo,CostoUnidad
        #0     1            2  3    4
        self.datosRetorno["fechafactura"] = self.grid1.GetCellValue(fila,0)
        self.datosRetorno["nrofactura"] = self.grid1.GetCellValue(fila,1)
        self.datosRetorno["idfactura"] = self.grid1.GetCellValue(fila,2)
        self.datosRetorno["en1oen2"] = self.grid1.GetCellValue(fila,3)
        self.datosRetorno["costo"] = self.grid1.GetCellValue(fila,4)
        self.Close()
